package com.kcsj.controller.basic;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.kcsj.domain.DelayedApplication;
import com.kcsj.domain.ProductModel;
import com.kcsj.domain.WarehouseOutRecord;
import com.kcsj.service.DelayedApplicationService;
import com.kcsj.service.ProductModelService;
import com.kcsj.service.WarehouseOutRecordService;
import com.kcsj.util.Condition;
import com.kcsj.util.JSONUtil;
import com.kcsj.util.MessageHelper;
import com.kcsj.util.Pagination;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

@WebServlet("/warehouseOutRecord.ctl")
public class WarehouseOutRecordController extends HttpServlet {
    private WarehouseOutRecordService warehouseOutRecordService = WarehouseOutRecordService.getInstance();


    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try{
            if (request.getParameter("productModelNo")==null){
                responseWarehouseOutRecords(response, null);
                return;
            }
            String productModelNo = request.getParameter("productModelNo");
            String warehouseNo = request.getParameter("wareHouseNo");
            List<Condition> conditionList = new ArrayList<>();
            conditionList.add(new Condition("productModelNo","=",productModelNo));
            responseWarehouseOutRecords(response, conditionList);
        }catch (SQLException e){
            response.getWriter().println(MessageHelper.dateBaseError());
            e.printStackTrace();
        }catch (Exception e){
            response.getWriter().println(MessageHelper.error());
            e.printStackTrace();
        }
    }

    private void responseWarehouseOutRecords(HttpServletResponse response,
                                               List<Condition> conditionList)
            throws ServletException, IOException, SQLException {
        Collection<WarehouseOutRecord> warehouseOutRecords =
                WarehouseOutRecordService.getInstance().
                        findAll(conditionList);
        String warehouseOutRecords_json =
                JSON.toJSONString(warehouseOutRecords, SerializerFeature.DisableCircularReferenceDetect);
        JSONObject data = MessageHelper.addData(warehouseOutRecords_json);
        response.getWriter().println(data);
    }
}
